﻿<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  
  <!-- If it is TeamCity run register TeamCity NUnit tests runner. -->
  <UsingTask TaskName="NUnit" AssemblyFile="$(teamcity_dotnet_nunitlauncher_msbuild_task)" Condition="Exists($(TeamCity))"/>
  <Choose>
    <When Condition="Exists($(TeamCity))">
    </When>
	  <Otherwise>
	    <PropertyGroup>
		    <NotTeamCity>true</NotTeamCity>
	    </PropertyGroup>
	  </Otherwise>
  </Choose>
  
  <!-- Default configuration of tools' paths -->
  <PropertyGroup Condition="$(ToolsCustomConfiguration)!=true">  
	  <!-- <NUnitPath>C:\Program Files\Programming\NET\NUnit\bin\net-2.0</NUnitPath> -->
	  <NUnitPath>..\tools\nunit</NUnitPath>
  </PropertyGroup>
  
  <!-- Determine reports directory -->
  <Choose>
	<!-- TeamCity -->
    <When Condition="Exists($(TeamCityArtifactDirectory))">
      <PropertyGroup>
        <ReportDirectory>$(TeamCityArtifactDirectory)</ReportDirectory>
      </PropertyGroup>      
    </When>	
	<!-- no CI -->
    <Otherwise>
      <PropertyGroup>
        <ReportDirectory>.\reports</ReportDirectory>
      </PropertyGroup>
    </Otherwise>
  </Choose>  

  <!-- Project specific configuration -->
  <PropertyGroup>
    <ProjectRoot>.\..\src</ProjectRoot>
	<SolutionFile>Patterns4Net.sln</SolutionFile>
	<TestAssemblies>
        $(ProjectRoot)/Patterns4Net.Tests/bin/Debug/Patterns4Net.Tests.dll;
    </TestAssemblies>
  </PropertyGroup>
  
  <ItemGroup>
	<SolutionRelease Include="$(ProjectRoot)\$(SolutionFile)">
	  <Properties>Configuration=Release</Properties>
	</SolutionRelease>
	<SolutionDebug Include="$(ProjectRoot)\$(SolutionFile)">
	  <Properties>Configuration=Debug</Properties>
	</SolutionDebug>	
  </ItemGroup>

    <Target Name="Build">
        <MSBuild Projects="@(ProjectToBuild)"/>
    </Target>  
  
  <!-- Main target: Build -->
  <Target Name="Build">        
    <MSBuild Projects="$(ProjectRoot)\$(SolutionFile)" Targets="Clean;Rebuild" Properties="Configuration=Debug"/>
    <CallTarget Targets="NUnitTests" ContinueOnError="true"/>
    <CallTarget Targets="MakeResults"/>
  </Target>  
  
  <!-- Main target: Release -->
  <Target Name="Release">        
	<MSBuild Projects="$(ProjectRoot)\$(SolutionFile)" Targets="Clean;Rebuild" Properties="Configuration=Release"/>
	<CallTarget Targets="CreateRelease"/>
  </Target>  
  
  <!-- ==================================== Release ========================================== -->
  <Target Name="CreateRelease">
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.Patterns/bin/Release/Patterns4Net.Attributes.dll"	
		DestinationFolder="release"/>  
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.Core/bin/Release/Patterns4Net.Core.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.PatternEnforcer.Core/bin/Release/PatternEnforcer.Core.dll"	
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.PatternEnforcer.MSBuildTask/bin/Release/PatternEnforcer.MSBuildTask.dll"	
		DestinationFolder="release"/>		
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.PatternEnforcer.Cmd/bin/Release/pattern-enforcer.exe"	
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/aexplorer.exe"			
		DestinationFolder="release"/>
		
	<!-- Configuration -->
	<ItemGroup>
		<ConfigFiles Include="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/Config/*.*" />
	</ItemGroup>
	<Copy
		SourceFiles="@(ConfigFiles)"
		DestinationFolder="release/Config"
    />	
		
	<!-- dependencies -->
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/Antlr3.Runtime.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/RibbonControlsLibrary.dll"		
		DestinationFolder="release"/>		
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/AvalonDock.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/Caliburn.Micro.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/Graphviz4Net.dll"			
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/Microsoft.Windows.Shell.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/System.Windows.Interactivity.dll"		
		DestinationFolder="release"/>
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.ArchitectureExplorer/bin/Release/WPFExtensions.dll"		
		DestinationFolder="release"/>			
			
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.Core/bin/Release/Mono.Cecil.dll"	
		DestinationFolder="release"/>		
	<Copy SourceFiles="$(ProjectRoot)/Patterns4Net.Core/bin/Release/Mono.Cecil.Rocks.dll"	
		DestinationFolder="release"/>				
  </Target>

  <!-- ==================================== Results ========================================== -->
  <!-- If any task want to fail the build, it must assign some varible 
    and than here conditionally raise an error -->
  <Target Name="MakeResults">
    <!-- NUnit tests -->
    <Error Text="NUnit tests failed!" Condition="'%(NUnitResult.Identity)'>0"/>
  </Target>
  
  <!-- ========================================== NUnitTests ======================================== -->  
  <Target Name="NUnitTests">    
    <NUnit
        Assemblies="$(TestAssemblies)"
        ExcludeCategory="LongRunning"
        OutputXmlFile="$(ReportDirectory)\nunit.xml"
        ToolPath="$(NUnitPath)">
      <Output TaskParameter="ExitCode" ItemName="NUnitResult"/>
    </NUnit>
  </Target>    
  
  <!-- ============================================ TransformReports ===================================== -->  
  <Target Name="TransformReports">
	<!-- NUnit, teamcity does not produce nunit.xml -->    
	<Xslt
		Inputs="$(ReportDirectory)\nunit.xml"
		Xsl="xsl/NUnit.xsl"
		Output="$(ReportDirectory)\nunit.html"
		Condition="Exists($(NotTeamCity))"
	/>
	<Xslt
		Inputs="$(ReportDirectory)\FxCop.xml"
		Xsl="xsl/FxCop.xsl"
		Output="$(ReportDirectory)\FxCop.html"    
		Condition="Exists($(NotTeamCity))"
	/>
  </Target>
</Project>